<?php

namespace app\admin\controller\wdsxh\mall;

use app\admin\model\wdsxh\mall\Express;
use app\admin\model\wdsxh\mall\Item;
use app\admin\model\wdsxh\mall\Logistics;
use app\admin\model\wdsxh\mall\RefundLog;
use app\admin\model\User;
use app\common\controller\Backend;

/**
 * 退款列表
 *
 * @icon fa fa-circle-o
 */
class Refund extends Backend
{
    
    /**
     * Refund模型对象
     * @var \app\admin\model\wdsxh\mall\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wdsxh\mall\Order;
        if ($this->auth->id == 1) {
            $expressObj = (new Express())->where('store_id','-1')->select();
        } else {
            $expressObj = (new Express())->where('admin_id',$this->auth->id)->select();
        }
        $this->view->assign('expressObj',$expressObj);
        $this->view->assign("buyNowList", $this->model->getBuyNowList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("refundStatusList", $this->model->getRefundStatusList());
        $this->view->assign("paidList", $this->model->getPaidList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    //同意申请
    public function apply($ids = null){
        $data=$this->model->get($ids);
        if ($data['status'] == 1){
            $data->refund_status='3';
            $data->save();
        }else{
            $data->refund_status='2';
            $data->save();
        }
        $this->success('操作成功');
    }

    //确认收货
    public function receipt($ids = null){
        $data=$this->model->get($ids);
        $data->refund_status='3';
        $data->save();
        $this->success('操作成功');
    }

  //退款
    public function refund($ids = null){
      $data = $this->model->get($ids);
      $refundObj = \app\admin\model\wdsxh\mall\Refund::where('order_id',$data->id)->find();
        if(!$data){
            $this->error('退款信息不存在');
        }
        $user=\app\admin\model\wdsxh\UserBelong::where('user_id',$data->user_id)->field('openid')->find();
        if(!$user){
            $this->error('用户信息不存在');
        }
       $mep = array(
           'user_id'=>$data->user_id,
            'refund_status' =>'3',
            'id'=>$data->id,
       );
        $order = \app\admin\model\wdsxh\mall\Order::where($mep)->find();
        $refundModel = \app\admin\model\wdsxh\mall\Refund::where('order_id',$data->id)->find();
        if(!$order){
            $this->error('支付订单信息不存在');
        }
        $refundLog = [
            'order_sn' => $data['order_no'],
            'refund_sn'=>$refundObj['refund_express_no'],
            'order_id' =>$data['id'],
            'status' => 1,
            'pay_fee'=>$data['pay_price'],
            'refund_fee' =>$refundObj['refund_price'],
            'createtime'=> time()
        ];
        $refundLogModel = [
            'order_sn' => $data['order_no'],
            'refund_sn'=>$refundObj['refund_express_no'],
            'order_id' =>$data['id'],
            'status' => -1,
            'pay_fee'=>$data['pay_price'],
            'refund_fee' =>$refundObj['refund_price'],
            'createtime'=> time()
        ];
        $refundSn=createOrder();
        if ($data->status == 1){
            $res=\addons\wdsxh\library\Wxapp::payRefund($order->order_no,$refundSn,$data->pay_price,array('refund_desc'=>'退款'));
        }elseif($data->status == 2){
            $many = $data->pay_price - $data->pay_postage;
            $res=\addons\wdsxh\library\Wxapp::payRefund($order->order_no,$refundSn,$many,array('refund_desc'=>'退款'));
        }else{
            $res=\addons\wdsxh\library\Wxapp::payRefund($order->order_no,$refundSn,$data->pay_price,array('refund_desc'=>'退款'));
        }
        if($res && $res['return_code'] == 'SUCCESS' && $res['result_code'] == 'SUCCESS'){
//            $order->refund_no=$refundSn;
            $order->complete_time=time();
            $order->refund_status='4';
            $order->save();
            $refundModel->refund_time=time();
            $refundModel->save();
            (new RefundLog())->save($refundLog);
            $this->success('退款操作成功');
        }else{
            (new RefundLog())->save($refundLogModel);
            $this->error('退款失败,错误信息：'.$res['return_msg']);
        }
    }


    //订单详情
    public function goods_details($ids = null){
        $orderObj = $this->model->get($ids);
        $logisticsModel = (new Logistics())->where('order_id',$orderObj['id'])->find();
        if ($logisticsModel == ''){
            $logisticsModel['delivery_no'] = '';
            $logisticsModel['delivery_id'] = '';
        }
        $usermodel = (new User())->where('id',$orderObj['user_id'])->find();
        $goodsObj = (new Item())->where('order_id',$orderObj['id'])->select();
        $refundMode = (new \app\admin\model\wdsxh\mall\Refund())->where('order_id',$orderObj['id'])->find();
        $this->view->assign("refundMode", $refundMode);
        $this->view->assign("goodsObj", $goodsObj);
        $this->view->assign("usermodel", $usermodel);
        $this->view->assign("orderObj", $orderObj);
        $this->view->assign("logisticsModel", $logisticsModel);
        return $this->view->fetch();
    }

}
